import { request } from 'umi';

const req_options = {
  method: {
    get: 'GET',
    post: 'POST',
  },
  headers: {
    accept: 'application/json',
    content: {
      form: 'application/x-www-form-urlencoded;charset=UTF-8',
      upload: 'multipart/form-data;charset=UTF-8',
    },
  },
};

const umiReq = async (url: string, params: any, method: string) => {
  delete params.type;
  if (method === req_options.method.get || method === req_options.method.post) {
    if (method === req_options.method.post) {
      return request(url, {
        method: req_options.method.post,
        body: JSON.stringify(params),
        headers: {
          Accept: req_options.headers.accept,
          'Content-Type': 'application/json;charset=UTF-8',
        },
      });
    } else {
      return request(url, {
        method: req_options.method.get,
        params: params
      });
    }
    // form
  } else {
    return request(url, {
      method: req_options.method.post,
      body: JSON.stringify(params),
      requestType: 'form',
      headers: {
        Accept: req_options.headers.accept,
        'Content-Type': 'application/json;charset=UTF-8',
      },
    });
  }
};

export default umiReq;
